package com.hnust.controller.paper.paperManagement;

import com.hnust.controller.MainController;
import com.hnust.domain.*;
import com.hnust.domain.dto.TeaNum;
import com.hnust.fx.FXMLController;
import com.hnust.service.PreviewPaperService;
import com.hnust.service.TestPaperService;
import com.hnust.service.impl.PreviewPaperServiceImpl;
import com.jfoenix.controls.JFXTabPane;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import lombok.SneakyThrows;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bouncycastle.util.encoders.UrlBase64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
@FXMLController
public class PaperListController implements Initializable {

    @Autowired
    private TestPaperService testPaperService;
    @Autowired
    private MainController mainController;
    @Autowired
    private PaperReviewController paperReviewController;
    @FXML
    private ScrollPane container;
    @FXML
    private AnchorPane Whole_pane;
    @FXML
    private AnchorPane part_top;
    @FXML
    private TextField part_top_subject;
    @FXML
    private TextField part_top_member;
    @FXML
    private Label label1;
    @FXML
    private Label label2;
    @FXML
    private HBox paper_hbox;
    @FXML
    private ImageView picture_search;
    @FXML
    private CheckBox  cbox111;
    @FXML
    private AnchorPane part_Main;


    public static Boolean transt;
    private static final Logger logger = LogManager.getLogger(PaperListController.class);
    //传给其它页面试卷ID
    static String Correct_Paper_ID,Preview_Paper_ID,Review_Paper_ID,Review_Test_ID,Review_Teacher_ID,Review_Test_Name,Result_Paper_ID,Result_Test_ID;
    static JFXTabPane tabPane;
    static CheckBox[] list_checkboxs0, list_checkboxs1, list_checkboxs2;
    static List<CheckBox> list_cbs_unstart, list_cbs_uncorrect, list_cbs_finished;
    static Tab tab1,tab0,tab2;
    static Integer I,index;
    //user_select_data是指用户在待开始、待批阅、已完成 三者界面之一中选择的数据
    private List<Test> mydata_unstart, mydata_need_correct, mydata_finished, user_select_data;

    @SneakyThrows
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        listenChange();
        user_select_data=new LinkedList<>();
        part_top.setPrefSize(1480, 170);
        part_top_subject.setPrefSize(300, 40);
        part_top_subject.setEditable(true);
        part_top_subject.setPromptText("操作系统");
        part_top_subject.setAlignment(Pos.CENTER_LEFT);
        part_top_subject.setPrefColumnCount(20);
        part_top_member.setPrefSize(300, 40);
        part_top_member.setEditable(true);
        part_top_member.setAlignment(Pos.CENTER_LEFT);
        part_top_member.setPrefColumnCount(20);
        paper_hbox.setSpacing(10);
        paper_hbox.setMargin(label1, new Insets(10, 10, 10, 10));
        paper_hbox.setMargin(label2, new Insets(10, 10, 10, 300));
        String url1 = "image/search.png";

        Image image = new Image(url1);
        picture_search.setImage(image);
//        获取待批阅试卷信息以及已完成试卷信息(1表示已完成，0表示待批阅)
        System.out.println("教师工号"+TeaNum.teaNum);
        // 获取待批阅所有试卷列表
        List<Test> paperLists = testPaperService.getNeedCorrectExams(TeaNum.teaNum).getData();
        System.out.println("paperLists="+paperLists);
        //    获取待开始的试卷列表
        List<Test> unStarted=testPaperService.getUnstartedExams(TeaNum.teaNum).getData();
        System.out.println("unStarted="+unStarted);
        dealData(paperLists,unStarted);
        index=1;
        list_cbs_unstart=new LinkedList<>();
        list_cbs_uncorrect=new LinkedList<>();
        list_cbs_finished=new LinkedList<>();
    }

    //由于从批阅界面点击结束批阅按钮跳过来之后，数据是不会重新加载的，所有设置此方法，在跳转页面之后调用此方法加载最新数据
    public void jumpFromPaperReview(){
        List<Test> paperLists = testPaperService.getNeedCorrectExams(TeaNum.teaNum).getData();
        System.out.println("paperLists="+paperLists);
        //    获取待开始的试卷列表
        List<Test> unStarted=testPaperService.getUnstartedExams(TeaNum.teaNum).getData();
        System.out.println("unStarted="+unStarted);
        dealData(paperLists,unStarted);
        index=1;
        list_cbs_unstart=new LinkedList<>();
        list_cbs_uncorrect=new LinkedList<>();
        list_cbs_finished=new LinkedList<>();
    }
    //    通过监听最外层容器的高宽度来改变内层Anchor的高宽度
    public void listenChange(){
        //通过监听最外层容器的宽度，来改变内层Anchor的宽度
        container.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                Whole_pane.setPrefWidth((double)newValue-2);
            }
        });
        //通过监听最外层容器的高度，来改变内层Anchor的高度
        container.heightProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                Whole_pane.setPrefHeight((double)newValue-2);
            }
        });

    }
    public void dealData(List<Test> paperLists,List<Test> unStartedList) {
        tabPane = new JFXTabPane();
        mydata_unstart = new ArrayList<Test>();  //需要显示的未开始考试数据放在了这里
        mydata_need_correct = new ArrayList<Test>();  //需要显示的未批阅考试数据放在了这里
        mydata_finished = new ArrayList<Test>();  //需要显示的已完成的考试数据放在了这里
        Iterator<Test> it = paperLists.iterator();

        while (it.hasNext()) {
            Test lh =it.next();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                long current_time = new Date().getTime();
                String cur_time = sdf.format(current_time);
                String exam_start_time = (String) lh.getBeginTime();
                String exam_end_time = (String) lh.getEndTime();
                if (cur_time.compareTo(exam_start_time) > 0) { //当前时间>考试结束时间----判断待批阅和已完成
                    if("1".equals(lh.getStatus())){
                        mydata_finished.add(lh);
                    }
                    else{
                        mydata_need_correct.add(lh);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        Iterator<Test> unStarted = unStartedList.iterator();
        while (unStarted.hasNext()) {
            Test lh =unStarted.next();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                long current_time = new Date().getTime();
                String cur_time = sdf.format(current_time);
                String exam_start_time = (String) lh.getBeginTime();
                String exam_end_time = (String) lh.getEndTime();
                if (cur_time.compareTo(exam_start_time) <= 0) { //当前时间<=考试开始时间----待开始
                    mydata_unstart.add(lh);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //另外定义函数启动页面显示
        getUnstarting2();
    }
    public void getUnstarting2() {
        int len0=mydata_unstart.size();
        System.out.println("len0"+len0);
        list_checkboxs0=new CheckBox[len0];
        for (int i=0;i<len0;i++){
            list_checkboxs0[i]=new CheckBox();
        }
        int len1=mydata_need_correct.size();
        System.out.println("len1"+len1);
        list_checkboxs1=new CheckBox[len1];
        for (int i=0;i<len1;i++){
            list_checkboxs1[i]=new CheckBox();
        }
        int len2=mydata_finished.size();
        System.out.println("len2"+len2);
        list_checkboxs2=new CheckBox[len2];
        for (int i=0;i<len2;i++){
            list_checkboxs2[i]=new CheckBox();
        }

        int p1=(mydata_unstart.size()+1)/itemsPerPage();
        if (p1==0) p1=1;
        int p2=(mydata_need_correct.size()+1)/itemsPerPage();
        if (p2==0) p2=1;
        int p3=(mydata_finished.size()+1)/itemsPerPage();
        if (p3==0) p3=1;
        pagination1.setPageCount(p1);
        pagination2.setPageCount(p2);
        pagination3.setPageCount(p3);

        pagination1.setCurrentPageIndex(0);
        pagination2.setCurrentPageIndex(0);
        pagination3.setCurrentPageIndex(0);

        pagination1.setPageFactory((Integer pageIndex) -> formPagination(mydata_unstart,0, pageIndex, list_cbs_unstart, list_checkboxs0));

        pagination2.setPageFactory((Integer pageIndex) -> formPagination(mydata_need_correct,2, pageIndex, list_cbs_uncorrect, list_checkboxs1));

        pagination3.setPageFactory((Integer pageIndex) -> formPagination(mydata_finished,1, pageIndex, list_cbs_finished, list_checkboxs2));



        tabPane.setCenterShape(true);
        tab0 = new Tab("                待开始");
        tab0.setContent(pagination1);

        tab1 = new Tab("待批阅");
        tab1.setContent(pagination2);
        if(tab1.isSelected()){
            System.out.println("待批阅被选择了");
        }
        tab2 = new Tab("已完成            ");

        tab2.setContent(pagination3);
        tab0.setClosable(false);
        tab1.setClosable(false);
        tab2.setClosable(false);

        tabPane.getTabs().addAll(tab0, tab1, tab2);

        part_Main.setTopAnchor(tabPane, 10.0);
        part_Main.setRightAnchor(tabPane, 10.0);
        part_Main.setBottomAnchor(tabPane, 10.0);
        part_Main.setLeftAnchor(tabPane, 10.0);
        part_Main.getChildren().add(tabPane);

    }
    public int itemsPerPage() {
        return 2;
    }

    static Pagination pagination1, pagination2, pagination3, pagination4;

    static {
        pagination1 = new Pagination();
        pagination2 = new Pagination();
        pagination3 = new Pagination();
    }

    public void getUnstarting(MouseEvent mouseEvent){ //这个是点击事件
        getUnstarting2();
    }


    public void pushItem(Test item){ //itemID是装着一条考试信息的最外层Box的ID
        user_select_data.add(item);
    }
    public void changeItemCB(String tabName, Boolean is_select){
        //改变内容中checkbox的状态
        if (tabName.equals("unstart")){
            for (int i=0;i<list_checkboxs0.length;i++){
                if(list_checkboxs0[i]!=null)
                    list_checkboxs0[i].setSelected(is_select);
            }
        }
        else if(tabName.equals("uncorrect")){
            for (int i=0;i<list_checkboxs1.length;i++) {
                if(list_checkboxs1[i]!=null)
                    list_checkboxs1[i].setSelected(is_select);
            }
        }
        else {
            for (int i=0;i<list_checkboxs2.length;i++) {
                if(list_checkboxs2[i]!=null)
                    list_checkboxs2[i].setSelected(is_select);
            }
        }
    }
    public void selectBox(){
        if(cbox111.isSelected()==true){
            //全选
            //删除选中item里重复存在的项
            List<Test>items=new LinkedList<>();
            if (tab0.isSelected()){
                items=mydata_unstart;
                changeItemCB("unstart",true);
            }
            if (tab1.isSelected()){
                items=mydata_need_correct;
                changeItemCB("uncorrect",true);
            }
            if (tab2.isSelected()){
                items=mydata_finished;
                changeItemCB("finished",true);
            }

            for (int i=0;i<items.size();i++){
                int flag=0;
                if(user_select_data.size()>0) {
                    //查询是否重复
                    for (int j = 0; j < user_select_data.size(); j++) {
                        if (items.get(i).getPapId().equals(user_select_data.get(j).getPapId())) {
                            flag=1;
                            break;
                        }
                    }
                    if (flag==0){
                        user_select_data.add(items.get(i));
                    }
                }
                else{
                    user_select_data.add(items.get(i));
                }
            }
        }
        else{ //取消了全选，则移除当前页面的所有
            List<Test>items=new LinkedList<>();
            if (tab0.isSelected()){
                items=mydata_unstart;
                changeItemCB("unstart",false);
            }
            if (tab1.isSelected()){
                items=mydata_need_correct;
                changeItemCB("uncorrect",false);
            }
            if (tab2.isSelected()){
                items=mydata_finished;
                changeItemCB("finished",false);
            }
            for (int i=0;i<user_select_data.size();i++){
                int flag=0;
                for(int j=0;j<items.size();j++){
                    if (items.get(j).getPapId().equals(user_select_data.get(i).getPapId())){
                        flag=1;
                        break;
                    }
                }
                if (flag==1&&user_select_data.size()>0){
                    user_select_data.remove(i);
                    i=-1;
                }
            }
        }
    }

    public VBox formPagination(List<Test> DATA,int type, int pageIndex,List<CheckBox> list_cbs, CheckBox[] arr_cbs) {
        VBox box = new VBox(5);
        int page = pageIndex * itemsPerPage();

        for (int i = page, j = 0; j < 2 && i < DATA.size(); i++, j++) {
            HBox one_pap = new HBox();//一张试卷的信息
            VBox mid_part = new VBox();//存储中间部分的全部数据
            HBox element = new HBox();//装中间的东西第一行
            VBox element_vb = new VBox();//右边下面的单行数据
            VBox right_all=new VBox();//包含了右边的按钮和修改试卷
            HBox right_part = new HBox();//右边的两个按钮
            VBox v_element = new VBox();//装图片
            v_element.setPadding(new Insets(100, 20, 40, 60));
            Image image = new Image("image/unstarting.png", 70, 50, false, false);
            Image image2 = new Image("image/finished.png", 70, 50, false, false);
            Image image3 = new Image("image/exam_doing.png", 70, 50, false, false);
            ImageView imageView = new ImageView();

            HBox label_hbox=new HBox();
            label_hbox.setPadding(new Insets(0,5,0,5));
            Label name = new Label((String) DATA.get(i).getName()+"  ");
            name.setPadding(new Insets(5,5,0,5));
            System.out.println("name="+name);
            name.setStyle("-fx-font-weight: bold;-fx-alignment: center");
            label_hbox.getChildren().addAll(name);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                long current_time = new Date().getTime();
                String cur_time = sdf.format(current_time);
                String exam_start_time = (String) DATA.get(i).getBeginTime();
                String exam_end_time = (String) DATA.get(i).getEndTime();
                if (type==0) { //当前时间<=考试开始时间----未开始
                    imageView.setImage(image);
                    int isSample=DATA.get(i).getIsSample();
                    Label is_sample = new Label("样卷");
                    is_sample.setStyle("-fx-text-fill: white;-fx-background-color: #3333cc;-fx-pref-width:80;-fx-pref-height: 30;-fx-background-radius: 4;-fx-alignment: center");
                    if (isSample==1) {
                        label_hbox.getChildren().add(is_sample);
                    }
                } else if(type!=0) {
                    if(DATA.get(i).getStatus().equals("1")){
                        imageView.setImage(image2);
                    }
                    else{
                        imageView.setImage(image3);
                    }
                }
                System.out.println("type="+type);
            } catch (Exception e) {
                e.printStackTrace();
            }
            v_element.getChildren().add(imageView);

            element.setPadding(new Insets(50, 200, 15, 0));

            HBox check_hbox=new HBox();
            check_hbox.setPadding(new Insets(5,5,0,5));

            arr_cbs[i].setId(i+"");
            arr_cbs[i].selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    String temp_arr=observable.toString().split(",")[0];
                    String arr_cbs_ID=temp_arr.split("=")[1];
                    Test item=DATA.get(Integer.parseInt(arr_cbs_ID));
                    if (newValue==true){
                        pushItem(item);
                    }
                    else{
                        for (int i=0;i<user_select_data.size();i++){
                            if (user_select_data.get(i).getPapId().equals(item.getPapId())){
                                user_select_data.remove(i);
                            }
                        }

                    }
                }
            });
            check_hbox.getChildren().add(arr_cbs[i]);
            Label exam_name = new Label("考试名称");
            exam_name.setStyle("-fx-text-fill: white;-fx-background-color: #3333cc;-fx-pref-width:100;-fx-pref-height: 30;-fx-background-radius: 4;-fx-alignment: center");
//            element.getChildren().addAll(exam_name,label_hbox);
//            HBox label_hbox=new HBox();
//            label_hbox.setPadding(new Insets(5,5,0,5));

//            Label time = new Label((String) DATA.get(i).getOpenTime()+"  ");
//            Label name = new Label((String) DATA.get(i).getName()+"  ");
//            time.setStyle("-fx-font-weight: bold;-fx-alignment: center");
//            name.setStyle("-fx-font-weight: bold;-fx-alignment: center");

//            label_hbox.getChildren().addAll(name);
            element.setStyle("-fx-spacing: 10");

//            int isSample=DATA.get(i).getIsSample();
//            Label is_sample = new Label("样卷");
//            is_sample.setStyle("-fx-text-fill: white;-fx-background-color: #3333cc;-fx-pref-width:80;-fx-pref-height: 30;-fx-background-radius: 4;-fx-alignment: center");
//            if (isSample==1) {
//                label_hbox.getChildren().add(is_sample);
//            }
            Label start_time = new Label("考试时间：" + DATA.get(i).getBeginTime()+"-"+DATA.get(i).getEndTime());
            Label author = new Label("创建人：" + DATA.get(i).getAuthor());
            Label create_time = new Label("创建时间：" + DATA.get(i).getCreateTime());
            element_vb.getChildren().addAll(start_time, author, create_time);
            element_vb.setPadding(new Insets(0, 300, 10, 35));

            right_part.setPadding(new Insets(70, 120, 10, 200));
            right_part.setStyle("-fx-spacing: 20");
            Button output = new Button("导出");
            output.setPrefSize(140, 42);
            output.setStyle("-fx-background-color: #eaeafa;-fx-text-fill: black;-fx-pref-height: 30;");

            Button preview = new Button("预览");
            preview.setPrefSize(140, 42);
            preview.setStyle("-fx-background-color: #3333cc;-fx-text-fill: white;-fx-pref-height: 30;");
            preview.setId(i+"");
            preview.setOnMouseClicked(event -> {
                String correct_paper_temp_ID=event.toString().split(",")[0];
                String correct_paper_ID=correct_paper_temp_ID.split("id=")[1];
                Preview_Paper_ID=DATA.get(Integer.parseInt(correct_paper_ID)).getPapId();
                PaperPreviewController.setText(Preview_Paper_ID);
                logger.info("预览试卷被点击了");
                mainController.skipView("预览试卷");
            });

            HBox correct_hbox=new HBox();
            correct_hbox.setPadding(new Insets(25,20,5,470));
            HBox correct_paper = new HBox();
            if(type==0){
                Label insert;
                insert=new Label("修改试卷");
                insert.setId(i+"");
                insert.setOnMouseClicked(event -> {
                    String correct_paper_temp_ID=event.toString().split(",")[0];
                    System.out.println("correct_paper_temp_ID"+correct_paper_temp_ID);
                    String correct_paper_ID=correct_paper_temp_ID.split("id=")[1];
                    System.out.println("correct_paper_ID"+correct_paper_ID);
                    Correct_Paper_ID=DATA.get(Integer.parseInt(correct_paper_ID)).getPapId();
                    PaperCorrectController.setText(Correct_Paper_ID);
                    logger.info("修改试卷被点击了");
                    mainController.skipView("试卷修改");
                });
                insert.setStyle("-fx-text-fill: blue;");
                correct_paper.getChildren().add(insert);
            }
           else if(type==1){
                Label insert1;
                insert1=new Label("导出批阅试卷");
                insert1.setStyle("-fx-text-fill: blue;");
                Label insert2;
                insert2=new Label("查看结果");
                insert2.setId(i+"");
                insert2.setOnMouseClicked(event -> {
                    String correct_paper_temp_ID=event.toString().split(",")[0];
                    String correct_paper_ID=correct_paper_temp_ID.split("id=")[1];
                    Result_Paper_ID=DATA.get(Integer.parseInt(correct_paper_ID)).getPapId();
                    Result_Test_ID=DATA.get(Integer.parseInt(correct_paper_ID)).getTestId();
                    String text=Result_Paper_ID+" "+Result_Test_ID;
                    System.out.println("text"+text);
                    PaperResultController.setText(text);
//                    System.out.println("correct_paper_ID"+correct_paper_ID);
//                    Correct_Paper_ID=DATA.get(Integer.parseInt(correct_paper_ID)).getPapId();
//                    PaperCorrectController.setText(Correct_Paper_ID);
                    logger.info("查看结果被点击了");
                    mainController.skipView("批阅结果");
                });
                insert2.setStyle("-fx-text-fill: orange;");
                correct_paper.setSpacing(50);
                correct_paper.getChildren().addAll(insert1,insert2);
            }
           else{
                Label insert;
                insert=new Label("批阅试卷");
                insert.setId(i+"");
                insert.setOnMouseClicked(event -> {
                    String correct_paper_temp_ID=event.toString().split(",")[0];
                    String correct_paper_ID=correct_paper_temp_ID.split("id=")[1];
                    Review_Paper_ID=DATA.get(Integer.parseInt(correct_paper_ID)).getPapId();
                    Review_Test_ID=DATA.get(Integer.parseInt(correct_paper_ID)).getTestId();
                    Review_Teacher_ID=DATA.get(Integer.parseInt(correct_paper_ID)).getTeacherId();
                    Review_Test_Name=DATA.get(Integer.parseInt(correct_paper_ID)).getName();
                    String text=Review_Paper_ID+" "+Review_Test_ID+" "+Review_Teacher_ID+" "+Review_Test_Name;
                    System.out.println("text"+text);
                    PaperReviewController.setText(text);
                    logger.info("批阅试卷被点击了");
                    mainController.skipView("试卷批阅");
                    paperReviewController.AddData();

                });
                insert.setStyle("-fx-text-fill: orange;");
                correct_paper.getChildren().add(insert);
            }
            correct_hbox.getChildren().add(correct_paper);

            right_part.getChildren().addAll(output, preview);
            right_all.getChildren().addAll(right_part,correct_hbox);


            Label line = new Label();
            line.setStyle("-fx-pref-width: 1840;-fx-pref-height: 4;-fx-background-color: #eaeafa");
            line.setPrefHeight(10);

            element.getChildren().addAll(check_hbox, exam_name, label_hbox);
//            if ((Integer) DATA.get(i).getIsSample() == 1) {
//                element.getChildren().add(is_sample);
//            }
            mid_part.getChildren().addAll(element, element_vb);
            one_pap.getChildren().addAll(v_element, mid_part, right_all);
            box.getChildren().addAll(one_pap, line);
        }
        return box;
    }

    public void change_selectall(){ //当选项卡切换时，取消选中当前选项卡的checkbox选中状态
        if (cbox111.isSelected()){
            //清空上一次设置样卷/取消样卷/删除 功能中保留的选中数据,同时将所有的checkbox选中状态取消
            user_select_data.removeAll(user_select_data);
            cbox111.setSelected(false);
        }
        if (tab0.isSelected()){
            changeItemCB("unstart",false);
        }
        if (tab1.isSelected()){
            changeItemCB("uncorrect",false);
        }
        if (tab2.isSelected()){
            changeItemCB("finished",false);
        }
    }
    public void getAutoPaper() {

        logger.info("用户点击自动生成试卷");
    }

    public void getHandlePaper() {
        logger.info("用户点击手动生成试卷");
        mainController.skipView("手动生成试卷");
    }

    public void changePaperType() {
        //应将参数设置为user_select_data
        ArrayList<String> paper_ids = new ArrayList<String>();
        for (int i=0;i<user_select_data.size();i++) {
            paper_ids.add((String) user_select_data.get(i).getPapId());
        }
        logger.info("用户点击设置样卷");
        Result<Object> result=testPaperService.changePaperType(paper_ids);
        if (result.getCode()==1) {
            logger.info("设置样卷成功");
            part_Main.getChildren().removeAll(tabPane);
            List<Test> paperLists = testPaperService.getNeedCorrectExams(TeaNum.teaNum).getData();
            List<Test> unStarted=testPaperService.getUnstartedExams(TeaNum.teaNum).getData();
            dealData(paperLists,unStarted);
            user_select_data.clear();
        } else
            logger.info("设置样卷失败");
        change_selectall();

    }

    public void removePaperType() {
        ArrayList<String> paper_ids = new ArrayList<String>();
        for (int i=0;i<user_select_data.size();i++) {
            paper_ids.add((String) user_select_data.get(i).getPapId());
        }
        logger.info("用户点击取消样卷");
        Result<Object> result=testPaperService.removePaperType(paper_ids);
        if (result.getCode()==1){
            logger.info("取消样卷成功");
            user_select_data.clear();
            part_Main.getChildren().removeAll(tabPane);
            List<Test> paperLists = testPaperService.getNeedCorrectExams(TeaNum.teaNum).getData();
            List<Test> unStarted=testPaperService.getUnstartedExams(TeaNum.teaNum).getData();
            dealData(paperLists,unStarted);
        }
        else
            logger.info("取消样卷失败");

        change_selectall();
    }

    public void deletePaper() {
        ArrayList<String> paper_ids = new ArrayList<String>();
        for (int i=0;i<user_select_data.size();i++) {
            paper_ids.add((String) user_select_data.get(i).getPapId());
        }
        logger.info("用户点击删除试卷");
        System.out.println("paper_id="+paper_ids);
        Result<Object> result=testPaperService.deletePaper(paper_ids);
        System.out.println(result);
        if(result.getCode()==1){
            logger.info("删除试卷成功");
            user_select_data.clear();
            part_Main.getChildren().removeAll(tabPane);
            List<Test> paperLists = testPaperService.getNeedCorrectExams(TeaNum.teaNum).getData();
            List<Test> unStarted=testPaperService.getUnstartedExams(TeaNum.teaNum).getData();
            dealData(paperLists,unStarted);
        }
        else
            logger.info("删除试卷失败");
        change_selectall();
    }

    public void searchPapers(MouseEvent mouseEvent) throws JSONException {
        String subject = part_top_subject.getText();
        String pap_name = part_top_member.getText();
        List<Test> paperLists = testPaperService.getNeedCorrectExams(TeaNum.teaNum).getData();
        List<Test> unStarted=testPaperService.getUnstartedExams(TeaNum.teaNum).getData();
        dealData(paperLists,unStarted);
        if(paperLists.size()==0){
            //没有查询到数据
            logger.info("没有查询到数据");
            Alert alert = new Alert(Alert.AlertType.ERROR, "没有查询到数据！");
            alert.showAndWait();
        }
        else{
            part_Main.getChildren().removeAll(tabPane);
            dealData(paperLists,unStarted);
        }

    }

}
